using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using MCMLXVII.BackEnd.Services.OrganizationChart.Messages;
using MCMLXVII.BackEnd.Data.OrganizationChart;
using MCMLXVII.BackEnd.Services.Common.Messages;
using MCMLXVII.BackEnd.Pipeline;
using MCMLXVII.BackEnd.Data.SystemDatabase;
using MCMLXVII.Core.Common.BackEnd;
using MCMLXVII.BackEnd.Data.OrganizationChart.DataSets;
using MCMLXVII.BackEnd.Data.SystemDatabase.DataSets;
using MCMLXVII.BackEnd.Services.OrganizationChart.Messages.AdvancedFind;
using MCMLXVII.Core.Common;

namespace MCMLXVII.BackEnd.Services.OrganizationChart.BusinessActions
{
    public class AdvancedFindActions
    {

        #region Constructors and Interfaces
        public AdvancedFindActions()
        {

        }


        #endregion


        /// <summary>Implements Action FindorganizationMemberByDescription</summary>
        public msgExtendedOrganizationMemberInfo GetOrganizationMemberExtended(msgGenericItemRequest<int> req)
        {
            int RequestingMemberID = Security.GetMemberIDFromSession();
            DSOrganizationTreeMemberInfo  ds = TreeMembers.GetOrganizationMemberInfo(req.Item, RequestingMemberID,false, false, true);
            if (ds.OrganizationMemberInfo.Rows.Count  == 1)
            {

                DSOrganizationTreeMemberInfo.OrganizationMemberInfoRow dr = (DSOrganizationTreeMemberInfo.OrganizationMemberInfoRow)ds.OrganizationMemberInfo.Rows[0];
                int HeadNodeID = 0;
                string HeadNodeName = "Not Found";
                TreeMembers.AdvancedGetHeadNodeID(req.Item, out HeadNodeID, out HeadNodeName);
                string dsname="";
                if (dr.SecurityProviderID > 0)
                {
                   DSSecurityProviders.SecurityProvidersAndClassRow spr =  Security.GetDomainInfo(dr.SecurityProviderID);
                   if (spr != null)
                       dsname = spr.NetBiosName;
                }
                return  new msgExtendedOrganizationMemberInfo(dr.MemberID, dr.Description, dr.NodeID, dr.MemberClass, XmlReader.Create(new System.IO.StringReader(dr.ClassProperties)), dr.SecurityProviderID, dsname ,  dr.LogonName, dr.DirectoryIdentifier, dr.Language, null, dr.UpdateSequence, HeadNodeID, HeadNodeName); 
            }
            else
                return null;
        }


        public msgMembershipInfo[] FindOrganizationMembers(msgOrganizationMemberFind  req)
        {
            int RequestingMemberID = Security.GetMemberIDFromSession();

            return TreeMembers.AdvancedFindMembersUnderHeadNode(req.HeadNodeID, req.Filter, req.IncludeFinalUsers, req.IncludeGroups, req.IncludeNodes, req.IncludeRoles, req.IncludeSecuritySelectables);
        }
    }
}
